/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "dal_rticonnextdds/converter/SampleInfoConverter.hpp"

TEST(test_sample_info_convert, sample_info_convert)
{
    dal_sample_info_t dest;
    DDS_SampleInfo src;

    // sample_state
    src.sample_state = DDS_READ_SAMPLE_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.sample_state, DAL_READ_SAMPLE_STATE);

    src.sample_state = DDS_NOT_READ_SAMPLE_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.sample_state, DAL_NOT_READ_SAMPLE_STATE);

    // view_state;
    src.view_state = DDS_NEW_VIEW_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.view_state, DAL_NEW_VIEW_STATE);

    src.view_state = DDS_NOT_NEW_VIEW_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.view_state, DAL_NOT_NEW_VIEW_STATE);

    // instance_state;
    src.instance_state = DDS_ALIVE_INSTANCE_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.instance_state, DAL_ALIVE_INSTANCE_STATE);

    src.instance_state = DDS_NOT_ALIVE_DISPOSED_INSTANCE_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.instance_state, DAL_NOT_ALIVE_DISPOSED_INSTANCE_STATE);

    src.instance_state = DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.instance_state, DAL_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE);

    // source_timestamp;
    src.source_timestamp.sec = 1;
    src.source_timestamp.nanosec = 100;
    src.disposed_generation_count = 1;
    src.no_writers_generation_count = 1;
    src.sample_rank = 1;
    src.generation_rank = 1;
    src.absolute_generation_rank = 1;

    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);

    EXPECT_EQ(dest.source_timestamp.sec, 1);
    EXPECT_EQ(dest.source_timestamp.nanosec, 100);
    EXPECT_EQ(dest.disposed_generation_count, 1);
    EXPECT_EQ(dest.no_writers_generation_count, 1);
    EXPECT_EQ(dest.sample_rank, 1);
    EXPECT_EQ(dest.generation_rank, 1);
    EXPECT_EQ(dest.absolute_generation_rank, 1);

    src.valid_data = true;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.valid_data, 1);

    src.valid_data = false;
    ASSERT_EQ(SampleInfoConverter::GetInstance()->ConvertSampleInfoFromRtiddsToDal(&dest, &src),
              DAL_RETCODE_OK);
    EXPECT_EQ(dest.valid_data, 0);
}